Skip to content

Fix onboarding private-domain history.replaceState loop past Safari's rate limit - #97709

Draft
MelvinBot wants to merge 1 commit into
mainfrom
claude-onboardingPrivateDomainReplaceStateLoop
Draft

Fix onboarding private-domain history.replaceState loop past Safari's rate limit#97709
MelvinBot wants to merge 1 commit into
mainfrom
claude-onboardingPrivateDomainReplaceStateLoop

Conversation

@MelvinBot

Copy link
Copy Markdown
Contributor

Explanation of Change

On /onboarding/private-domain, two redirectors disagreed about where a validated private-domain user belongs, so they bounced that user back and forth. Each bounce is one history.replaceState; Mobile Safari caps that at 100/10s and throws SecurityError: Attempt to use history.replaceState() more than 100 times per 10 seconds. The rate limit is the symptom — the bug is the loop.

  • The screen pushes them forward. BaseOnboardingPrivateDomain redirects validated users off this screen (to Workspaces if there are joinable policies, else Purpose/Employees) via forceReplace.
  • The flow router pulled them back. getOnboardingInitialPath only redirected public-domain validated users away from private-domain; validated private-domain users with accessible policies fell through and got the stale initialPath (/onboarding/private-domain) returned, so useOnboardingFlowRouter reset them right back onto the screen.

Screen says "leave", router said "stay" → ping-pong → replaceState spam → Safari SecurityError.

This PR makes the two agree:

  1. Primary (removes the bounce source)OnboardingFlow.ts: when the last-visited path is /onboarding/private-domain and the account is validated, the router now resolves forward — Workspaces when there are accessible policies (mirroring the screen), otherwise Purpose/Employees — instead of returning the stale private-domain path. The existing guard only covered public-domain validated users; it now also covers validated private-domain users. Unvalidated users still stay on the screen to confirm their code.
  2. Defensive (caps the blast radius)BaseOnboardingPrivateDomain.tsx: a useRef "hasRedirected" guard so the forced-replace redirect fires at most once per mount, bounding replaceState calls even if a re-mount slips through.

Unit tests were added to tests/unit/OnboardingFlowTest.ts covering the new router branches (validated private-domain → Workspaces / Purpose / Employees, and unvalidated private-domain staying put).

Fixed Issues

$ #97473
PROPOSAL: #97473 (comment)

Tests

// TODO: The human co-author must fill out the manual tests they ran before marking this PR as "ready for review".
// Suggested flow: sign up with a private email domain, proceed onboarding to /onboarding/private-domain, validate the account, and confirm the app redirects forward cleanly (to the join-workspace / purpose step) without the URL flapping between routes and with no SecurityError / history.replaceState errors in the console.
// Note: automated unit tests for the router logic were added in tests/unit/OnboardingFlowTest.ts and pass.

  • Verify that no errors appear in the JS console

Offline tests

QA Steps

// TODO: The human co-author must fill out the QA steps before marking this PR as "ready for review", or the issue title must include "[No QA]."

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari

…ate limit

Co-authored-by: Joe Ph <gijoe0295@users.noreply.github.com>
@MelvinBot

Copy link
Copy Markdown
Contributor Author

The failing check is unrelated to this PR — it's a pre-existing flaky UI test. No code change is needed here.

Failing checks breakdown:

  • test (job 6)tests/ui/WorkspaceWorkflowsLoadMoreTest.tsx:200: "shows every match and hides the Load more button while searching"Expected: 10, Received: 11. This is a workspace approval-workflow pagination/search test with no connection to this PR. This PR only touches onboarding: OnboardingFlow.ts, BaseOnboardingPrivateDomain.tsx, and tests/unit/OnboardingFlowTest.ts.
  • checklist (PR Author Checklist) → expected; the human co-author hasn't ticked the author-checklist boxes yet. Not something I fix.
Why it's flaky, not caused by this PR
  • The test types "Member" into the workflows search box and asserts the debounced filter narrows 11 workflows down to 10 (dropping the default "Everyone" workflow) inside a waitFor. The WorkspaceWorkflowsPage search is debounced, so the assertion is timing-sensitive — in this run the filter hadn't settled (Received: 11) before waitFor gave up. The sibling test directly above it (revealing all 11 cards) passed.
  • The other 7 of 8 test shards passed, along with Storybook, Bun tests, ESLint, typecheck, knip, and Oxfmt — the only red beyond the checklist is this one shard on this one timing-dependent assertion.
  • The PR changes nothing under src/pages/workspace/workflows/ and shares no module with this test.

Per policy I don't fix unrelated/flaky failures — re-running test (job 6) should clear it.

@MelvinBot

Copy link
Copy Markdown
Contributor Author

No code fix is warranted — the failure is a pre-existing flaky test unrelated to this PR. The fix is to re-run the failed job.

The head commit is unchanged (e833c0b) and the failures are the same two as before:

Action: re-run the failed job — Re-run failed jobs on run 30835265733. It should go green. I don't push code changes for unrelated/flaky failures per policy.

Why it's a flake, not a regression from this PR
  • The test types "Member" into the workflows search box and asserts a debounced filter narrows 11 workflows to 10 (dropping the default "Everyone" workflow) inside a waitFor. Because the search is debounced, the assertion is timing-sensitive — in this run the filter hadn't settled (Received: 11) before waitFor gave up. The sibling test right above it passed.
  • 7 of 8 test shards passed, along with typecheck, ESLint, knip, Storybook, Bun, and Oxfmt. The new OnboardingFlowTest.ts unit tests pass.
  • The test file (WorkspaceWorkflowsLoadMoreTest.tsx) has been on main since the June 2026 Load-more pagination work and shares no module with anything this PR changed. Its last change (2026-06-19, "Harden Load more negative assertions") was itself a flakiness-hardening pass.

@mountiny

mountiny commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

@gijoe0295 how is it looking?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants